﻿$(document).ready(function () {
	RegisterPersonPicker();
	OnBtnChooseClick();
	OnIDChanged();
	OnPersonNameClick();
	OnPersonPickerCancelClick();
	OnLocationChanged();
});

function RegisterPersonPicker() {

	var elementObject = $("#txtPersonPicker");

	$(elementObject).on("keyup", function (e) {
		if ($(elementObject).val().length != 0) {
			if (e.keyCode != 13 || e.keyCode == null) {
				clearTimeout($(elementObject).data("person-list-timer"));

				$(elementObject).data("person-list-timer", setTimeout(function () {

					var elementOffset = $(elementObject).offset();

					var tooltipPos = new Object();

					tooltipPos.top = elementOffset.top + 25;
					tooltipPos.left = elementOffset.left;

					var requestParams = new Object();
					requestParams.requestData = new Object();

					requestParams.requestData.nameSequence = $.trim($(elementObject).val());
					requestParams.requestData.isRestricted = $("#rbMogilev").is(":checked");
					requestParams.requestUrl = $(elementObject).data("list-url");

					ShowHidePersonPickerMask("show");
					ShowHidePersonPickerSpinner("show");

					$.ajax({
						url: requestParams.requestUrl,
						type: "POST",
						data: requestParams.requestData,
						complete: function (xhr, textStatus) { OnPersonListLoadComplete(xhr, textStatus); }
					});

				}, 400));
			} else if (e.keyCode == 13 && $("#personListContainer .selectedPerson").length != 0) {
				$("#btnChoosePerson").trigger("click");
			}
		}
	});

	$(elementObject).on("paste", function () {
		setTimeout(function () { $(elementObject).trigger("keyup"); },100);
	});
}

function OnBtnChooseClick(onChoosing) {

	$("#btnChoosePerson").off("click");

	$("#btnChoosePerson").on("click", function () {

		onChoosing();

		ShowHideMaskForm("hide", $("#personPicker"));
	});
}

function OnIDChanged() {

	$("#hfPickedPersonID").on("changed", function () {

		var requestData = new Object();

		requestData.personID = $(this).val();

		ShowHidePersonPickerMask("show");
		ShowHidePersonPickerSpinner("show");

		$.ajax({
			url: $("#txtPersonPicker").data("person-info-url"),
			type: "POST",
			data: requestData,
			complete: function (xhr, textStatus) { OnPersonInfoLoadComplete(xhr, textStatus, "#personInfoContent"); }
		});
	});
}

function OnPersonNameClick() {

	$("#pickSection").on("click", ".dropDownElement", function () {

		$("#pickSection .selectedPerson").removeClass("selectedPerson");

		$(this).addClass("selectedPerson");

		$("#hfPickedPersonID").val($(this).data("person-id"));
		$("#hfPickedPersonID").trigger("changed");
	});

}

function OnPersonPickerCancelClick() {

	$("#btnCancelPersonPick,#personPicker .deleteIcon").on("click", function () {
		ShowHideMaskForm("hide", $("#personPicker"));
	});
}

function OnLocationChanged() {

	$("#rbMogilev, #rbAnywhere").on("change", function () {
		$("#txtPersonPicker").trigger("keyup");
	});

}

function OnPersonListLoadComplete(xhr, textStatus) {

	if (textStatus == "success") {

		if ($("#personListContainer").is(":hidden")) {
			$("#personListContainer").show();
		}

		var personList = xhr.responseText;

		$("#personListContainer").html(personList);

		var selectedPerson = $("#pickSection .selectedPerson");

		if ($(selectedPerson).length != 0) {
			$(selectedPerson).trigger("click");
		}
	} else {
		ShowHidePersonPickerMask("hide");
		ShowHidePersonPickerSpinner("hide");
		alert(GetErrorMsg(textStatus));
	}
}
function OnPersonInfoLoadComplete(xhr, textStatus, target) {

	if (textStatus == "success") {
		$(target).html(xhr.responseText);

		if ($("#personInfoContainer").data("not-found").toLowerCase() == "true") {
			$("#btnChoosePerson").attr("disabled", "disabled");
		}
		else {
			$("#btnChoosePerson").removeAttr("disabled");
		}

		ShowHidePersonPickerMask("hide");
		ShowHidePersonPickerSpinner("hide");
	} else {
		alert(GetErrorMsg(textStatus));
	}
}

function GetPickedPersonID() {

	return $("#hfPickedPersonID").val();
}

function GetSelectedPersonName() {

	return $("#personListContainer .selectedPerson").text();
}

function SetPerson(name, id) {

	if (id== 0) {
		$("#hfPickedPersonID").val(0);
	} else {
		$("#txtPersonPicker").val(name);
		$("#hfPickedPersonID").val(id);
	}

	$("#hfPickedPersonID").trigger("changed");
}

function ShowPersonPicker() {
	ClearPersonPicker();
	ShowHideMaskForm("show", $("#personPicker"));
}

function ClearPersonPicker() {

	$("#rbMogilev").attr("checked", "checked");
	$("#txtPersonPicker").val("");
	$("#personListContainer").empty().hide();
	$("#personInfoContent").empty();
	$("#hfPickedPersonID").val(0);
	$("#btnChoosePerson").attr("disabled", "disabled");

}
function ShowHidePersonPickerMask(mode) {

	var maskWidth = $("#personPicker").outerWidth(false);
	var maskHeight = $("#personPicker").outerHeight(false) - $("#personPicker .popupTitle").outerHeight(false);

	$("#personPickerMask").css({ width: maskWidth + "px", height: maskHeight + "px", opacity: "0.5" });

	if (mode == "show") {

		$("#personPickerMask").show();

	} else {

		$("#personPickerMask").hide();
	}
}
function ShowHidePersonPickerSpinner(mode) {

	if (mode == "show") {
		$("#pickerSpinner").show();
	} else {
		$("#pickerSpinner").hide();
	}
}